<template>
  <div class="root">
    <div>
      <h3 style="font-weight: normal; margin-left: 18px">创建考试</h3>
    </div>

    <el-form>

    <!-- 基本信息 -->
      <!-- 标题 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">1</div>
          &nbsp;&nbsp;
          <div class="word">基本信息</div>
        </div>
      </div>
      <!-- 内容 -->
      <el-form class="content" :model="adddata">
        <el-form-item label="考试名称" prop="title" class="item-conten" 
          :rules="[
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur',
          },]">
          <el-input style="width: 300px" v-model="adddata.title" />
        </el-form-item>
        <el-form-item label="考试说明">
          <el-input maxlength="300" show-word-limit type="textarea" style="width: 450px" v-model="adddata.info" />
        </el-form-item>
      </el-form>

    <!-- 内容设置 -->
      <!-- 标题 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">2</div>
          &nbsp;&nbsp;
          <div class="word">内容设置</div>
        </div>
      </div>
      
      <!-- 内容 -->
      <div class="content">

        <el-form-item label="考试内容:" prop="title" class="content_item" :model="adddata" :rules="[
          {
            required: true,
            message: '请设置内容',
            trigger: 'blur',
          },]">

          <div class="box-card">
            <div class="card-header">
              <h3>试题列表</h3>
            </div>
            <div class="functionRight">
              <div>总分：{{totalPoints}}</div>
              <div>已添加&nbsp;{{lenLang}}&nbsp;题</div>
              <div>
                <el-button @click="empty">清空</el-button>
              </div>
            </div>
          </div>

              <!-- 考试内容外边的左边 -->

                <div class="topic" v-if="adddata.questions.length>0 || compile!==undefined">

                  <div class="munse" v-if="quest1.length>0">
                    <p style="margin-left:10px">多选题{{quest1.length}}道</p> <!--$event获取input输入的数据-->
                    <p style="margin-left:10px">每题<el-input @input="ipt($event,'多选题')" v-model="multiple"
                        style="width:40px;margin:0 5px 0 5px"></el-input>分</p>
                  </div>

                  <div class="munse2" v-if="quest2.length>0">
                    <p style="margin-left:10px">单选题{{quest2.length}}道</p>
                    <p style="margin-left:10px">每题<el-input @input="ipt($event,'单选题')" v-model="Multiple"
                        style="width:40px;margin:0 5px 0 5px"></el-input>分</p>
                  </div>
                  
                  <div class="munse2" v-if="quest5.length>0">
                    <p style="margin-left:10px">问答题{{quest5.length}}道</p>
                    <p style="margin-left:10px">每题<el-input @input="ipt($event,'问答题')" v-model="FAQs"
                        style="width:40px;margin:0 5px 0 5px"></el-input>分</p>
                  </div>

                  <div class="munse2" v-if="quest3.length>0">
                    <p style="margin-left:10px">判断题{{quest3.length}}道</p>
                    <p style="margin-left:10px">每题<el-input @input="ipt($event,'判断题')"
                        style="width:40px;margin:0 5px 0 5px"></el-input>分</p>
                  </div>

                  <div class="munse2" v-if="quest4.length>0">
                    <p style="margin-left:10px">填空题{{quest4.length}}道</p>
                    <p style="margin-left:10px">每题<el-input @input="ipt($event,'填空题')"
                        style="width:40px;margin:0 5px 0 5px"></el-input>分</p>
                  </div>

                </div>



          <!-- 考试内容 -->

          <el-scrollbar max-height="400px"> <!--滚动条-->

            <div class="exam">

              <div v-for="(item,index) in questions" :key="item.id">

                <!-- 头部 -->
                <div class="exam_top0">

                  <div class="exam_top">
                    <div>{{index+1}}. {{ item.type }}</div>
                    <span>分值：</span>
                    <el-input class="input" v-model="item.scores" />
                  </div>
                  
                  <!--icon-->
                  <div class="icon">
                    <span class="span" @click="amend(item)"> <!--修改-->
                      <el-icon><EditPen /></el-icon>
                    </span>
                    <span class="span2" @click="del(index)"> <!--单删-->
                      <el-icon><Delete /></el-icon>
                    </span>
                  </div>

                </div>

                <!-- 题干 -->
                <div v-html="item.title"></div>

                <!-- 单选题 -->
                <div class="exam_question" v-if="item.type=='单选题'" v-for="items in item.answers" :key="items.id">
                  <div :class="item.answer == items.answerno ? 'xuanItem yes' : 'xuanItem'">
                    <div class="radio" />
                    <div>{{ items.answerno }}:&nbsp;{{ items.content }}</div>
                  </div>
                </div>

                <!-- 多选题 -->
                <div class="exam_question" v-if="item.type=='多选题'" v-for="items in item.answers" :key="items.id">
                  <div :class="item.answer.includes(items.answerno)? 'xuanItem yes': 'xuanItem'">
                    <div class="check" />
                    <div>{{ items.answerno }}:&nbsp;{{ items.content }}</div>
                  </div>
                </div>

                <!-- 判断题 -->
                <div class="ifs" v-if="item.type == '判断题'">
                  <span> 正确答案： </span>
                  <span>
                    {{ item.answer }}
                  </span>
                </div>

                <!-- 填空题 -->
                <div class="" v-if="item.type == '填空题'">
                  <div class="ifs">
                    <span> 正确答案： </span>
                    <span>
                      {{ item.answer }}
                    </span>
                  </div>
                  <div class="analysis">
                    <span> 答案解析： </span>
                    <span>
                      {{ item.explains }}
                    </span>
                  </div>
                </div>

                <!-- 问答题 -->
                <div class="analysis" v-if="item.type == '问答题'">
                  <span> 答案解析： </span>
                  <span>
                    {{ item.explains }}
                  </span>
                </div>
                
              </div>

            </div>

          </el-scrollbar>


          <div class="bottom">
            <el-button  @click="addTest">添加题目</el-button>
            <el-button @click="bulkImport">批量导入</el-button>
            <el-button @click="questionAdd">从题库选择</el-button>
            <el-button @click="parperAdd">选择已有试题</el-button>
          </div>

          <el-form-item label="试题存入题库" prop="title" style="margin-top: 20px">
            <div>
              <el-select v-model="selectVal" class="m-2" placeholder="请选择题库">
                <el-option v-for="item in bankArr" :key="item.id" :label="item.title" :value="item.id" />
              </el-select>
              <el-button style="margin-left: 18px" @click="Addquestion">
                <span style="font-size: 18px">＋</span>
                <span style="font-size: 13px">创建试题库</span>
              </el-button>
            </div>
          </el-form-item>
        
        </el-form-item>

      </div>

      <!-- 考试设置 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">3</div>
          &nbsp;&nbsp;
          <div class="word">考试设置</div>
        </div>
      </div>
      <!-- 内容 -->
      <div class="content3">

        <el-form-item label="通过分数" prop="goNumber" style="margin-top: 20px" :model="adddata" :rules="[
          {
            required: true,
            message: '请输入分数',
            trigger: 'blur',
          },]">
          <el-input type="number" v-model="goNumber" style="width: 100px" />
        </el-form-item>

        <el-form-item label="考试时长" style="margin-top: 20px">
          <el-radio-group v-model="testTime" class="ml-4">
            <el-radio label="1" size="large">不限制时长</el-radio>
            <el-radio label="2" size="large">限制时长</el-radio>
          </el-radio-group>
          <div v-show="testTime == 2">
            <el-input v-model="adddata.limittime" style="width: 80px; margin-left: 20px" />&nbsp;分钟
          </div>
        </el-form-item>

        <el-form-item label="开放时间" style="margin-top: 20px; width: 625px">
          <div class="time">
            <div>
              <el-date-picker v-model="OpenTime" type="datetimerange" :shortcuts="shijian.Arrshor" range-separator="至"
                start-placeholder="开始时间" format="YYYY-MM-DD HH:mm" value-format="YYYY-MM-DD HH:mm"
                end-placeholder="结束时间" @change="timeChange"/>
            </div>
            <div class="span">不填表示永久</div>
          </div>
        </el-form-item>

        <el-form-item label="答案解析" style="margin-top: 20px">
          <el-radio-group v-model="Radio" class="radio">
            <el-radio label="1" size="large">交卷后显示</el-radio>
            <el-radio label="2" size="large">不允许查看</el-radio>
            <el-radio label="3" size="large">仅可查看对错</el-radio>
            <el-radio label="4" size="large">仅查看错题</el-radio>
            <el-radio label="5" size="large">考试结束后查看</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="防作弊" style="margin-top: 20px">
          <el-checkbox v-model="Disturbance" label="试题顺序打乱" size="large" @change="zbchange" />
          <el-checkbox v-model="option_Disturbance" label="选项顺序打乱(单选题，多选题，判断题)" @change="optionChange" />
        </el-form-item>

      </div>

      <!-- 教师范围 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">4</div>
          &nbsp;&nbsp;
          <div class="word">教师范围</div>
        </div>
      </div>
      <div class="content">
        <el-form-item label="可见老师" prop="seeTeacher" style="margin-top: 20px">
          <el-button style="margin-left: 18px" @click="teacherAdd"><span style="font-size: 18px">＋</span><span
              style="font-size: 13px">选择</span>
            <div class="wrod">
              {{ lenthNO1 }}
            </div>
          </el-button>
        </el-form-item>
      </div>


      <!-- 考试学生 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">5</div>
          &nbsp;&nbsp;
          <div class="word">考试学生</div>
        </div>
      </div>
      <div class="content">
        <el-form-item label="考生范围" prop="seeTeacher" style="margin-top: 20px">
          <el-button style="margin-left: 18px" @click="studentTest"><span style="font-size: 18px">＋</span><span
              style="font-size: 13px">选择</span>
            <div class="wrod">
              {{ lenthNO2 }}
            </div>
          </el-button>
        </el-form-item>
      </div>


      <!-- 协同设置 -->
      <div class="itemNumber">
        <div class="titles">
          <div class="num">6</div>
          &nbsp;&nbsp;
          <div class="word">协同设置</div>
        </div>
      </div>
      <div class="content">
        <el-form-item label="阅卷老师" prop="seeTeacher" style="margin-top: 20px">
          <el-button style="margin-left: 18px" @click="exam_marker"><span style="font-size: 18px">＋</span><span
              style="font-size: 13px">选择</span>
            <div class="wrod">
              {{ lenthNO3 }}
            </div>
          </el-button>
        </el-form-item>

        <el-form-item style="margin-top: 50px;margin-left: 100px;">
          <el-button type="primary" v-if="compile==undefined" @click="release">发布</el-button>
          <el-button type="primary" v-if="compile!==undefined" @click="relecom">编辑</el-button>
          <el-button style="margin-left: 18px" @click="released">保存(不发布)</el-button>
          <el-button style="margin-left: 18px" @click="release_cancel">取消</el-button>
        </el-form-item>
      </div>

    </el-form>


    <!-- 添加题目抽屉 -->
    <AddTestVue :addTestShow="addTestShow" v-if="addTestShow == true" @qx="qx"  @adds="adds" :updArr="updArr" :title="title" @drawerClose="drawerClose" ></AddTestVue>

    <!-- 导入 -->
    <uploadFiles v-if="Updatadialog" :UrL="UrL" v-model="Updatadialog" @updataFile="updataFile"></uploadFiles>

    <!-- 题库选择弹窗 -->
    <Question_bank :questionBool="questionBool" @queshandleClose="queshandleClose" @quesConfirm="quesConfirm" @quesCancel="quesCancel"></Question_bank>

    <!-- 选择已有试题 -->
    <testPaperList :isshow="isshow" @paperhandleClose="paperhandleClose" @paperConfirm="paperConfirm" @paperCancel="paperCancel"></testPaperList>

    <!-- 创建题库 -->
    <el-dialog v-model="dialogFormVisible" title="添加题库" width="30%">
      <BaseList v-if="dialogFormVisible" @close="closefn" @refresh="refreshfn" :id="id">
      </BaseList>
    </el-dialog>

    <!-- 可见老师 -->
    <seeTeacher :popup="popup" :params="params" @handleClose="handleClose" @confirm2="confirm2" @cancel2="cancel2" ></seeTeacher>

    <!-- 学生范围 -->
    <testStudentVue :showStudent="showStudent" :params2="params2" @StudenthandleClose="StudenthandleClose" @StudentConfirms="StudentConfirms" @StudentCancels="StudentCancels"></testStudentVue>

    <!-- 阅卷老师 -->
    <yueteacher :popover="popover"  :params3="params3" @handleCloses="handleCloses" @confirms3="confirms3" @cancels3="cancels3"></yueteacher>

  </div>
</template>
<script lang="ts" setup>
import moment from 'moment';
import AddTestVue from '../../components/test/addTest.vue' //添加题目抽屉
import uploadFiles from "../../components/test/uploadFiles.vue";
import Question_bank from '../../components/test/Question_bank.vue' //题库选择弹窗
import testPaperList from '../../components/test/testPaperList.vue' //已有试题弹窗
import BaseList from "../../components/test/BaseList.vue";  //创建题库
import seeTeacher from '../../components/test/seeTeacher.vue' //可见老师
import testStudentVue from '../../components/test/testStudentVue.vue' //学生范围
import yueteacher from "../../components/test/yueteacher.vue"; //阅卷老师

import { EditPen, Delete } from '@element-plus/icons-vue';
import { ref,onMounted,reactive,provide,watchEffect,computed } from 'vue';
import { database_list,test_Add,test_get } from "../../api/test"; //接口
import { ElMessage } from 'element-plus';
import route from '../../router/router'

// 接收编辑跳转过来的参数
import { useRoute } from 'vue-router';

const Route = useRoute(); //获取到值
onMounted(() => {
  console.log("获取到的参数", Route.query);  //接收到的参数
})



let selectVal = ref('') // 题库选中的


// 考试设置
let testTime:any = ref('1')
let time:any = ref(0)  // 多少分钟
onMounted(() => { // 判断考试时长等不等于1 显示或隐藏
  if (time.value == 0) {
    testTime.value = '1';
  } else {
    testTime.value = '2';
  }
});


// 考试设置 开放时间
let OpenTime:any = ref([])
let shijian:any = reactive({
  Arrshor: [
    {
      text: '最近一个星期',
      value: () => {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
        return [start, end];
      },
    },
    {
      text: '最近一个月',
      value: () => {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        return [start, end];
      },
    },
    {
      text: '最近三个月',
      value: () => {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
        return [start, end];
      },
    },
  ],
})

// 考试设置 答案解析
let Radio = ref('1') //默认选中第一个


// _________________________________________考试内容左侧 多少道题
let quest1:any = ref([]) // 多少道多选题
let quest2:any = ref([]) // 多少道单选题
let quest3:any = ref([]) // 多少道判断题
let quest4:any = ref([]) // 多少道填空题
let quest5:any = ref([]) // 多少道问答题
// _________________________________________

// 添加题目弹窗 -----------------------------------
let addTestShow = ref(false) // 谈窗默认关闭

// 取消按钮
const qx = (val: any) => {
  console.log(val);
  addTestShow.value = val;
};
// 确定按钮
let questions:any = ref([])
const adds = (bool: boolean, val:any) => {
  console.log('添加题目',val);
  addTestShow.value = bool;

  if (title.value == '修改') {
    questions.value = questions.value.map((item: any) => {
      // console.log(item);
      if (item.id == val.id) { //相等的时候覆盖掉原本的数据
        item = val;            //重新赋值
      }
      return item;
    });
  } else {
    questions.value.push(val);//数据
  }
  



  // 考试内容左侧的 多少道题
  quest1.value = questions.value.type === '多选题'
  quest3.value = questions.value.type === '判断题'
  quest4.value = questions.value.type === '填空题'
  quest2.value = questions.value.type === '单选题'
  quest5.value = questions.value.type === '问答题'

};


// 试题列表 清空按钮
let empty = () => {
  adddata.questions =[]
}


// 标题
let title = ref('')

// 点击添加题目
let addTest =() => {
  title.value = '添加';
  addTestShow.value = true;
}

// 差号关闭右侧弹窗
const drawerClose = (bool: boolean) => {
  addTestShow.value = bool;
};


// 考试内容 点击删除当前一条数据
let del = (index:any) => {
  // console.log(index);
  questions.value.splice(index, 1);
}

// 父传子数据
let updArr:any = ref([]) //赋值的
// 考试内容 点击修改
let amend = (val:any) => {
  // console.log(val);
  title.value = '修改'
  addTestShow.value = true  //弹窗打开
  updArr.value = JSON.stringify(val); //赋值
}
// 添加题目功能结束----------------------------------------


// 批量导入-------------------------------------------------

let UrL = ref('http://estate.eshareedu.cn/exam/api/test/upload')

let Updatadialog = ref(false) //弹窗 默认关闭
const bulkImport=()=>{
  Updatadialog.value=true //弹窗打开
}

// 批量导入子组件传来数据 
const updataFile=(e:any)=>{
  console.log('批量导入传来数据',e);
  questions.value=e

  adddata.questions = e  // 点击发布 添加赋值


  // 考试内容左侧的 多少道题
  quest1.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '多选题') //过滤 只拿多选题
  quest3.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '判断题')//过滤 只拿判断题
  quest4.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '填空题')//过滤 只拿填空题
  quest2.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '单选题')//过滤 只拿单选题
  quest5.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '问答题')//过滤 只拿问答题

}


// 批量导入结束-------------------------------


// input事件---------------------------------

let multiple = ref('')// 多选题输入的
let Multiple = ref('')// 单选题输入的
let FAQs = ref('')// 问答题输入的

const ipt = (e: any, type: string) => {
  // console.log(e, type); //分数，类型
  if (type == '多选题') {
    questions.value.map((item: any) => {
      if (item.type == '多选题') {
        item.scores = e
      }
    })
  } else if (type == '单选题') {
    questions.value.map((item: any) => {
      if (item.type == '单选题') {
        item.scores = e
      }
    })
  } else if (type == '填空题') {
    questions.value.map((item: any) => {
      if (item.type == '填空题') {
        item.scores = e
      }
    })
  } else if (type == '判断题') {
    questions.value.map((item: any) => {
      if (item.type == '判断题') {
        item.scores = e
      }
    })
  } else {
    questions.value.map((item: any) => {
      if (item.type == '问答题') {
        item.scores = e
      }
    })
  }
}
// input结束-------------------------------


// 计算总分
const totalPoints = computed(() => {
  let sum = 0;
  for (let i in questions.value) {
    sum = sum + Number(questions.value[i].scores);
  }
  return sum;
});


// 题库选择弹窗开始----------------------------------------

let questionBool = ref(false) //弹窗 默认关闭
// 点击题库选择按钮开启弹窗
let questionAdd = () => {
  questionBool.value = true //弹窗开启
}

// 题库弹窗点击差号关闭弹窗
let queshandleClose = (val:any) => {
  questionBool.value = val
}

// 点击确定按钮
let quesConfirm = (val:any) => {
  questionBool.value = val
}

// 点击取消按钮
let quesCancel = (val:any) => {
  questionBool.value =val
}

// 题库选择弹窗结束----------------------------------------



// 选择已有试题弹窗--------------------------------------------------
let isshow = ref(false) //弹窗 默认关闭

let parperAdd = () => {
  isshow.value = true //点击开启弹窗
}

// 差号
let paperhandleClose = (val:any) => {
  isshow.value = val
}




// 确定按钮
let paperConfirm = (val:any,data:any)=>{
  // console.log(data.value.questions);
  
  isshow.value = val //关闭弹窗

  questions.value=data.value.questions //子组件传过来的数据

  adddata.questions = data.value.questions // 点击发布 添加赋值

  // 考试内容左侧的 多少道题
  quest1.value = questions.value.map((item: any) => item).filter((items: any) => items.type == '多选题') //过滤 只拿多选题
  quest3.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '判断题')//过滤 只拿判断题
  quest4.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '填空题')//过滤 只拿填空题
  quest2.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '单选题')//过滤 只拿单选题
  quest5.value =  questions.value.map((item: any) => item).filter((items: any) => items.type == '问答题')//过滤 只拿问答题

}

// 取消按钮
let paperCancel = (val:any) => {
  isshow.value = val
}



// 题库下拉框数据----------------------------------------------------------------

let bankArr:any = ref([]) //赋值的

let getdatabase_list = async() => {
  let res = await database_list({})
  console.log(res);
  bankArr.value = res.data.list
}
getdatabase_list()


// 创建题库

// 题库
const dialogFormVisible = ref(false);

let Addquestion = () => {
  id.value = 0;
  getdatabase_list()
  dialogFormVisible.value=true
}

 // 取消 关闭对话框
 const closefn = (e: boolean) => {
  dialogFormVisible.value =e 
  getdatabase_list()
  console.log("取消");
 };

 const id = ref(0);
const refreshfn = (e: boolean) => {
  dialogFormVisible.value = e;
  getdatabase_list()
 };


// 通过分数---------------------------------------------
let goNumber = ref(60);

// 获取到开始和结束的时间
let timeChange = (val: any) => {
  adddata.begintime = moment(val[0]).format('YYYY-MM-DD hh:mm:ss'); //开始
  adddata.endtime = moment(val[1]).format('YYYY-MM-DD hh:mm:ss'); //结束
  // console.log(begintime.value,endtime.value);
};


// 防作弊

// 试题顺序打乱
let Disturbance = ref(false);
// 选项顺序打乱
let option_Disturbance = ref(false)

// 试题顺序打乱
let checkone = ref(0)
const zbchange = (val: any) => {
  if (val == true) {
    checkone.value = 1;
    adddata.qorder = checkone.value // 点击发布 添加赋值
  } else {
    checkone.value = 0;
  }
};

// 选项顺序打乱
let checktwo = ref(0)
let optionChange = (val: any) => {
  // console.log(val);
  if (val === true) {
    checktwo.value = 1;
    adddata.aorder = checktwo.value  // 点击发布 添加赋值
  } else {
    checktwo.value = 0;
  }
};


// 可见老师--------------------------------------------------


// 点击弹窗
let popup = ref(false) //弹窗 默认关闭
let teacherAdd = () => {
  popup.value = true; //弹窗开启
}

// 差号关闭弹窗
let handleClose = (val:any) => {
  popup.value = val
}

let params = ref([]) //数据赋值的
let lenthNO1 =ref(0) //多少条
// 确定按钮
let confirm2 = (val:any,para:any) => {
  popup.value = val // 弹窗关闭
  params.value = para //子组件传过来的数据
  lenthNO1.value = params.value.length; //多少条

  adddata.limits = para  // 点击发布 添加赋值
}

// 取消按钮
let cancel2 = (val:any) => {
  popup.value = val
}


// 学生范围----------------------------------------------------
let showStudent = ref(false) //弹窗默认关闭
// 点击打开弹窗
let studentTest = () => {
  showStudent.value = true;
}

// 差号关闭弹窗
let StudenthandleClose = (val:any) => {
  showStudent.value = val
}

// 确定按钮
let params2 = ref([]) //数据赋值的
let lenthNO2 =ref(0) //多少条
let StudentConfirms = (val:any,param:any) => {
  showStudent.value = val //弹窗关闭
  params2.value = param //数据
  lenthNO2.value = params2.value.length //多少条数据
  // console.log(params2.value);

  adddata.students = param  // 点击发布 添加赋值
}

// 取消按钮
let StudentCancels = (val:any) => {
  showStudent.value = val
}



// 阅卷老师----------------------------

let popover = ref(false) //弹窗 默认关闭
// 点击开启弹窗
let exam_marker = () => {
  popover.value = true //弹窗开启
}

// 差号关闭弹窗
let handleCloses = (val:any) => {
  popover.value = val
}

// 确定按钮
let params3 = ref([]) //数据赋值的
let lenthNO3 =ref(0) //多少条

let confirms3 = (val:any,par:any) =>{
  popover.value = val // 弹窗关闭
  params3.value = par //数据
  lenthNO3.value = params3.value.length //多少条数据

  adddata.markteachers = par  // 点击发布 添加赋值
}

// 取消按钮
let cancels3 = (val:any) => {
  popover.value = val
}




// 孙子组件传过来的数据 试题列表
let infos = ref([]) //赋值的
let lenLang = ref(0) //多少道题
provide('infotwo', infos); //接收孙子传过来的数据
watchEffect(()=>{   //监听数据变化
  if(infos.value){
    infos.value.map((item: any) => questions.value.push(item)); //把数据赋值给questions.value
  }
  lenLang.value = questions.value.length; //多少道题

  // 防作弊
  if (checkone.value == 1) { //试题顺序打乱
    Disturbance.value = true;
  } else {
    Disturbance.value = false;
  }
  if (checktwo.value == 1) {  // 选项顺序打乱
    option_Disturbance.value = true;
  } else {
    option_Disturbance.value = false;
  }
})



// 点击发布
// 参数
let adddata:any = reactive({
    id: 0,
    title: '', //考试名称
    info: '', //考试说明
    admin: sessionStorage.getItem('name'), //创建人
    begintime: '', //开始时间 
    endtime: '', //结束时间 
    limittime: time.value,  //分钟 
    scores: 100, //计算总分
    pastscores: goNumber.value, //通过分数
    qorder: checkone.value, //试题打乱
    aorder: checktwo.value, //选项打乱
    answershow: Number(Radio.value), //答案解析
    isshow: 1,
    databaseid: 20,
    state: 1,
    limits: params.value, //可见老师
    students: params2.value, //学生范围
    markteachers: params3.value, //阅卷老师
    questions: questions.value, //试题列表
  })

let release = async () => {
  console.log(adddata);

  let res: any = await test_Add(adddata);
  console.log(res);
  if (res.errCode == 10000) {
    // 跳转页面
    route.push('./test')

    ElMessage({
      message: '添加成功',
      type: 'success',
    });
  } else {
    ElMessage({
      message: '添加失败',
      type: 'error',
    });
  }
};


// 保存不发布
let released = async () => {
  // console.log(adddata);
  adddata.state = 2
  let res: any = await test_Add(adddata);
  console.log(res);
  if (res.errCode == 10000) {
    // 跳转页面
    route.push('./test')

    ElMessage({
      message: '保存成功',
      type: 'success',
    });
  } else {
    ElMessage({
      message: '保存失败',
      type: 'error',
    });
  }


}


// 取消发布
let release_cancel =() => {
  route.push('./test')
}



//编辑------------------------------------
// 判断是添加还是修改
let compile:any = ref(null) 
compile.value = Route.query.id;

// 数据回显
let gettest_get = async() => {
  // console.log(Route.query.id);
  let res:any = await test_get({id:Route.query.id})
  console.log(res);
  
  if(res.errCode===10000){
    adddata.title = res.data.title; //考试名称
    adddata.info = res.data.info;   //考试说明
    questions.value = res.data.questions //试题列表
    adddata.goNumber = res.data.pastscores //通过分数
    adddata.limittime = res.data.limittime //分钟
    OpenTime.value.push(res.data.begintime)  //开始时间
    OpenTime.value.push(res.data.endtime)  //结束时间
    adddata.answershow = res.data.answershow //答案解析
    checktwo.value = res.data.aorder //选项打乱
    checkone.value = res.data.qorder //试题打乱
    lenthNO1.value = res.data.limits.length //可见老师长度
    lenthNO2.value = res.data.students.length //学生范围长度
    lenthNO3.value = res.data.markteachers.length //阅卷老师长度
    adddata.limits = res.data.limits
    adddata.students = res.data.students
    adddata.markteachers = res.data.markteachers
  }

}
gettest_get()

// 修改
let relecom = async() => {

  adddata.id = compile.value
  adddata.questions = questions.value //试题列表
  adddata.questions.checktwo = checktwo.value  //选项打乱
  adddata.questions.checkone = checkone.value  //试题打乱
  let res: any = await test_Add(adddata);
  console.log(res);
  if (res.errCode == 10000) {
    // 跳转页面
    route.push('./test')

    ElMessage({
      message: '修改成功',
      type: 'success',
    });
  } else {
    ElMessage({
      message: '修改失败',
      type: 'error',
    });
  }
  
}



</script>
<style lang="less" scoped>
.itemNumber {
        background-color: #f9faff;
        width: 100%;
        height: 60px;

        .titles {
            margin-top: 20px;
            margin-left: 15px;
            display: flex;

            .num {
                font-size: 35px;
                color: #c7e5ff;
                line-height: 60px;
            }

            .word {
                line-height: 60px;
            }
        }
    }

    .content{
      width: 370px;
      margin-left: 60px;
    }
    .box-card {
        width: 100%;
        display: flex;
        justify-content: space-between;
        border: 1px solid #dcdfe6;
        padding: 5px 10px;
    .functionRight {
          width: 300px;
          display: flex;
          justify-content: space-around;
      }
  }
  .bottom {
      width: 100%;
      border-left: 1px solid #dcdfe6;
      border-right: 1px solid #dcdfe6;
      border-bottom: 1px solid #dcdfe6;
      padding: 15px 10px;
  }
  .time{
    display: flex;
  }
  .span{
    margin-left: 10px;
    color: rgb(172, 167, 167);
  }

  .content3{
      width:1000px;
      margin-left: 60px;
    }
  .radio{
    display: flex;
  }
  .wrod {
  padding: 2px 3px;
  color: white;
  font-size: 12px;
  background-color: #409eff;
  border-radius: 17px;
  position: relative;
  top: -14px;
  right: -21px;
}
.exam{
  // width: 100%;
  padding-top: 10px;
  padding-left: 10px;
  padding-right: 20px;
  border-left: 1px solid #dcdfe6;
  border-right: 1px solid #dcdfe6;
}
.exam_top{
  display: flex;
  div{
    margin-right: 20px;
  }
  .input{
    width: 60px;
    height: 26px;
    margin-top: 3px;
  }
}
.exam_top0{
  display: flex;
  justify-content: space-between;
}

.icon{
  .span{
    font-size: 18px;
    color: #409eff;
  }
  .span2{
    margin-left: 10px;
    font-size: 18px;
    color: #409eff;
  }
}

.exam_question {
  .xuanItem {
    display: flex;
    margin: 10px 0;

    .radio {
      border: 1px solid #e7f0ed;
      height: 20px;
      width: 20px;
      border-radius: 15px;
      background-color: #f5f5f5;
      margin-left: 15px;
      margin-right: 9px;
      margin-top: 6px;
    }

    .check {
      border: 1px solid #e7f0ed;
      height: 20px;
      width: 20px;
      border-radius: 3px;
      background-color: #f5f5f5;
      margin-left: 15px;
      margin-right: 9px;
      margin-top: 6px;
    }
  }

  .yes {
    width: 100%;
    background-color: #eefaf6;
  }
}

.ifs {
  width: 98%;
  height: 40px;
  background-color: #eefaf6;
  line-height: 40px;
  margin-top: 15px;
  color: #5acda6;
  padding: 0 10px;
}

.analysis {
  width: 98%;
  height: 40px;
  background-color: #f5faff;
  line-height: 40px;
  margin-top: 15px;
  color: #d0d6db;
  padding: 0 10px;
}

.el-scrollbar{
  width: 100%;
  height: auto;
}

.topic{
  position: absolute;
  top:55px;
  left: -126px;
  border: 1px solid #dcdfe6;
  padding-right: 13px;
  padding-bottom: 7px;
}

.content_item{
  margin-top: 20px;
  width: 1000px
}
.item-conten{
  margin-top: 20px
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 10px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}
</style>
